package com.xiaoshuidi.cloud.module.finance.convert.finance;

import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.kingdee.core.dto.TransferConfirmDTO;
import com.xiaoshuidi.cloud.module.contract.vo.OrderFeeDetailVO;
import com.xiaoshuidi.cloud.module.contract.vo.OrderInvoicePageRespVO;
import com.xiaoshuidi.cloud.module.finance.api.finance.dto.BankTransferReqDTO;
import com.xiaoshuidi.cloud.module.finance.api.finance.dto.FinanceAccountDTO;
import com.xiaoshuidi.cloud.module.finance.api.finance.dto.FinanceMoneyFlowDTO;
import com.xiaoshuidi.cloud.module.finance.api.finance.dto.FinancePaymentDTO;
import com.xiaoshuidi.cloud.module.finance.api.finance.dto.*;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.account.*;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.flow.FinanceMoneyFlowExcelVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.flow.FinanceMoneyFlowRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.flow.FinanceOrderFeeVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.reconciliation.FinanceReconciliationExportVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.reconciliation.FinanceReconciliationFlowExcelVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.reconciliation.FinanceReconciliationRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.reconciliation.FinanceReconciliationSummaryVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.finance.vo.transfer.*;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.IssueInvoicePageRespExcelVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.IssueInvoicePageRespVO;
import com.xiaoshuidi.cloud.module.finance.controller.admin.invoice.vo.OrderInvoicePageRespExcelVO;
import com.xiaoshuidi.cloud.module.finance.controller.app.finance.vo.AppFinanceAccountWithdrawReqVO;
import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.*;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.Map;

@Mapper
public interface FinanceConvert {
    FinanceConvert INSTANCE = Mappers.getMapper(FinanceConvert.class);

    FinanceMoneyFlowDO convert(FinancePaymentDTO reqDTO);

    @Mapping(target = "statusDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.order" +
            ".FinanceCommonStatusEnum.getDesc(financeMoneyFlowDO.getStatus()))")
    @Mapping(target = "sourceFromDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.finance" +
            ".PaymentSourceEnum.getDesc(financeMoneyFlowDO.getSourceFrom()))")
    @Mapping(target = "actualPaymentAmount",expression = "java((financeMoneyFlowDO.getTradeAmount()" +
            "!=null?financeMoneyFlowDO.getTradeAmount(): java.math.BigDecimal.ZERO).subtract(\n" +
            "                (financeMoneyFlowDO.getDiscount()!=null?financeMoneyFlowDO.getDiscount(): java.math" +
            ".BigDecimal.ZERO)))")
    FinanceMoneyFlowRespVO convert(FinanceMoneyFlowDO financeMoneyFlowDO);

    List<FinanceMoneyFlowRespVO> convertList(List<FinanceMoneyFlowDO> list);
    List<FinanceMoneyFlowRespDTO> convertAppList(List<FinanceMoneyFlowDO> list);

    PageResult<FinanceMoneyFlowRespVO> convertToFinanceMoneyFlowPage(PageResult<FinanceMoneyFlowDO> pageResult);

    List<FinanceMoneyFlowExcelVO> convertToFinanceMoneyFlowExcelList(List<FinanceMoneyFlowDO> list);

    List<OrderInvoicePageRespExcelVO> exportInvoiceListByEntity(List<OrderInvoicePageRespVO> list);

    List<IssueInvoicePageRespExcelVO> exportIssueInvoiceList(List<IssueInvoicePageRespVO> list);

    List<FinanceReconciliationFlowExcelVO> convertToFinanceMoneyFlowExcelList2(List<FinanceMoneyFlowDO> list);

    @Mapping(target = "direction", expression = "java(flow.getDirection() == 0 ? \"流入\":\"流出\")")
    @Mapping(target = "payTime", expression = "java(flow.getPayTime() != null ? flow.getPayTime().toLocalDate() : " +
            "null)")
    @Mapping(target = "status", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.order" +
            ".FinanceCommonStatusEnum.getDesc(flow.getStatus()))")
    @Mapping(target = "roomRentOrderPeriod", expression = "java(flow.rentOrderPeriod())")
    @Mapping(target = "tradeAmount", expression = "java(flow.getTradeAmount().subtract(flow.getDiscount()))")
    FinanceReconciliationFlowExcelVO map2(FinanceMoneyFlowDO flow);

    @Mapping(target = "subject", expression = "java(obj.getSubject() == 0 ? \"租客\":\"企业\")")
    @Mapping(target = "direction", expression = "java(obj.getDirection() == 0 ? \"流入\":\"流出\")")
    @Mapping(target = "sourceFrom", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.finance.PaymentSourceEnum.getDesc(obj.getSourceFrom()))")
    @Mapping(target = "tradeTime", expression = "java(obj.getTradeTime() != null ? obj.getTradeTime().toLocalDate() : null)")
    @Mapping(target = "payTime", expression = "java(obj.getPayTime() != null ? obj.getPayTime().toLocalDate() : null)")
    @Mapping(target = "tradeNo", expression = "java(org.springframework.util.StringUtils.hasText(obj" +
            ".getMerchantOrderId()) ? obj.getMerchantOrderId() : obj.getTradeNo())")
    @Mapping(target = "amount", expression = "java(obj.getTradeAmount().subtract(obj.getDiscount()))")
    FinanceMoneyFlowExcelVO map(FinanceMoneyFlowDO obj);

    @Mapping(target = "jbPerson", source = "operator")
    @Mapping(target = "ywlbNumber", source = "businessTypeNo")
    @Mapping(target = "payeeBankAcct", source = "payeeBankAccount")
    @Mapping(target = "payeeProvence", source = "payeeBankProvince")
    @Mapping(target = "payeeCity", source = "payeeBankCity")
    @Mapping(target = "mdNumber", source = "apartmentId")
    @Mapping(target = "department", source = "apartmentName")
    TransferConfirmDTO convert(BankTransferReqDTO reqDTO);

    @Mapping(target = "department", source = "apartmentName")
    @Mapping(target = "apartmentId", source = "apartmentId")
    FinanceBankTransferDO convert2(BankTransferReqDTO reqDTO);

    FinanceBankTransferDO convert3(FinanceBankTransferReqVO reqVO);

    @Mapping(target = "jbPerson", source = "operator")
    @Mapping(target = "ywlbNumber", source = "businessTypeNo")
    @Mapping(target = "payeeBankAcct", source = "payeeBankAccount")
    @Mapping(target = "payeeProvence", source = "payeeBankProvince")
    @Mapping(target = "payeeCity", source = "payeeBankCity")
    TransferConfirmDTO convert(FinanceBankTransferReqVO reqVO);

    PageResult<FinanceBankTransferRespVO> convertToFinanceBankTransferPage(PageResult<FinanceBankTransferDO> pageResult);

    PageResult<FinanceAccountVO> convertToFinanceAccountPage(PageResult<FinanceAccountDO> page);

    BankTransferReqDTO convert(FinanceAccountRefundReqVO reqVO);

    PageResult<FinanceAccountJournalVO> convertToFinanceAccountJournalPage(PageResult<FinanceAccountJournalExtDO> pageResult);

    @Mapping(target = "tradeTypeDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.finance.AccountTradeTypeEnum.getDesc(journal.getTradeType()))")
    @Mapping(target = "statusDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.order.FinanceCommonStatusEnum.getDesc(journal.getStatus()))")
    FinanceAccountJournalVO map(FinanceAccountJournalDO journal);

    @Mapping(target = "tradeTypeDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.finance.AccountTradeTypeEnum.getDesc(journal.getTradeType()))")
    @Mapping(target = "statusDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.order.FinanceCommonStatusEnum.getDesc(journal.getStatus()))")
    FinanceAccountJournalVO map(FinanceAccountJournalExtDO journal);

    @Mapping(target = "name", source = "custName")
    FinanceAccountVO convert(FinanceAccountDO accountDO);

    List<FinanceAccountVO> convertToFinanceAcountVOList(List<FinanceAccountDO> list);

    List<FinanceAccountJournalExcelVO> convertToFinanceAccountJournalExcelList(List<FinanceAccountJournalExtDO> list);

    @Mapping(target = "tradeTypeDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.finance.AccountTradeTypeEnum.getDesc(journal.getTradeType()))")
//    @Mapping(target = "statusDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.order.FinanceCommonStatusEnum.getDesc(journal.getStatus()))")
//    @Mapping(target = "direction", expression = "java(journal.getDirection() == 0 ? \"收入\" : \"支出\")")
    FinanceAccountJournalExcelVO map2(FinanceAccountJournalExtDO journal);

    List<FinanceBankTransferExcelVO> convertToFinanceBankTransferExcelVO(List<FinanceBankTransferDO> list);

    @Mapping(target = "status", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.order.BankTransferStatusEnum.getDesc(entity.getStatus()))")
    @Mapping(target = "isChangeDesc", expression = "java(entity.getIsChange() == null?\"\":(entity.getIsChange()==0?\"否\":\"是\"))")
    FinanceBankTransferExcelVO map(FinanceBankTransferDO entity);

    @Mapping(target = "statusDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.order.BankTransferStatusEnum.getDesc(entity.getStatus()))")
    @Mapping(target = "isChangeDesc", expression = "java(entity.getIsChange() == null?\"\":(entity.getIsChange()==0?\"否\":\"是\"))")
    FinanceBankTransferRespVO map2(FinanceBankTransferDO entity);

    @Mapping(target = "settlementDate", expression = "java(day.getSettlementDate().toString())")
    FinanceReconciliationSummaryVO convert(FinanceReconciliationLogDO day);

    PageResult<FinanceReconciliationRespVO> convertToFinanceReconciliationPage(PageResult<FinanceYinlianReconciliationPair> page);

    default FinanceReconciliationRespVO convert(FinanceYinlianReconciliationPair pair) {
        FinanceReconciliationRespVO resp = new FinanceReconciliationRespVO();
        if (pair.getFlow() != null) {
            resp.setLeft(convertLeft(pair));
        }
        if (pair.getYinlian() != null) {
            resp.setRight(convertRight(pair));
        }
        resp.setSuccess(pair.getChecked() == 1);
        resp.setRemark(pair.getRemark());
        return resp;
    }
    @Mapping(target ="roomRentOrderNo", source = "flow.roomRentOrderNo")
    @Mapping(target ="merchantOrderId", source = "flow.merchantOrderId")
    @Mapping(target ="totalAmount", source = "flow.tradeAmount")
    @Mapping(target ="renter", source = "flow.renter")
    FinanceReconciliationRespVO.Left convertLeft(FinanceYinlianReconciliationPair left);
    @Mapping(target = "flowNo", source = "flow.flowNo")
    @Mapping(target = "payMethod", source = "flow.payMethod")
    @Mapping(target = "settleAmount", source = "yinlian.settleAmount")
    @Mapping(target = "serviceFee", source = "yinlian.serviceFee")
    FinanceReconciliationRespVO.Right convertRight(FinanceYinlianReconciliationPair left);

    FinanceAccountDTO convert2(FinanceAccountDO financeAccountDO);

    List<FinanceReconciliationExportVO> convertToFinanceReconciliationExportVO(List<FinanceYinlianReconciliationPair> list);

    @Mapping(target = "apartmentName", source = "flow.apartmentName")
    @Mapping(target ="roomRentOrderNo", source = "flow.roomRentOrderNo")
    @Mapping(target ="merchantOrderId", source = "flow.merchantOrderId")
    @Mapping(target ="amount", source = "flow.tradeAmount")
    @Mapping(target ="renter", source = "flow.renter")
    @Mapping(target = "flowNo", source = "flow.flowNo")
    @Mapping(target = "payMethod", source = "flow.payMethod")
    @Mapping(target = "settleAmount", source = "yinlian.settleAmount")
    @Mapping(target = "serviceFee", source = "yinlian.serviceFee")
    @Mapping(target = "result", expression = "java(pair.getChecked() == 1 ? \"√\" : \"×\")")
    FinanceReconciliationExportVO map(FinanceYinlianReconciliationPair pair);

    @Mapping(target = "jbPerson", source = "operator")
    @Mapping(target = "ywlbNumber", source = "businessTypeNo")
    @Mapping(target = "payeeBankAcct", source = "payeeBankAccount")
    @Mapping(target = "payeeProvence", source = "payeeBankProvince")
    @Mapping(target = "payeeCity", source = "payeeBankCity")
    @Mapping(target = "mdNumber", source = "apartmentId")
    @Mapping(target = "department", source = "department")
    @Mapping(target = "businessScene", source = "businessType")
    TransferConfirmDTO convert(FinanceBankTransferDO financeBankTransferDO);

    void convert(AppFinanceAccountWithdrawReqVO reqVO,
                 @MappingTarget FinanceAccountWithdrawDO.FinanceAccountWithdrawDOBuilder builder);

    FinanceWalletApplyRefundDetailsVO convert1(BankTransferReqDTO bankTransferReqDTO);

    List<FinanceAccountWithdrawExcelVO> convertToWithdrawExcelList(List<FinanceAccountWithdrawExtDO> dtolist);

    @Mapping(target = "result", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.finance" +
            ".FinanceAccountWithdrawResultEnum.getDesc(entity.getResult()))")
    FinanceAccountWithdrawExcelVO convertToWithdrawExcel(FinanceAccountWithdrawExtDO entity);

    @Mapping(target = "bizTypeDesc", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.finance" +
            ".SourceBizTypeEnum.getDesc(journal.getBizType()))")
    @Mapping(target = "isEnjambment", expression = "java(com.xiaoshuidi.cloud.module.finance.controller.admin.finance" +
            ".vo.transfer.FinanceBankTransferPayMentExcelVO.getIsEnjambment(journal.getPayeeBank()))")
    FinanceBankTransferPayMentExcelVO map5(FinanceBankTransferDO journal);

    List<FinanceBankTransferPayMentExcelVO> convertToFinanceBankTransferPayMentExcelVO(List<FinanceBankTransferDO> list);

    FinanceMoneyFlowDTO convertApiDO(FinanceMoneyFlowDO flowDO);

    List<FinanceMoneyFlowDTO> convertApiDOList(List<FinanceMoneyFlowDO> flowDO);
    FinanceMoneyFlowRespDTO convertRespDTO(FinanceMoneyFlowDO financeMoneyFlowDO);

    FinancePaymentChangeUpdateVO convertLogDo(FinancePaymentChangeLogDO flowDO);

    @Mapping(target = "status", expression = "java(com.xiaoshuidi.cloud.module.finance.enums.order.BankTransferStatusEnum.getDesc(entity.getStatus()))")
    @Mapping(target = "isChange", expression = "java(entity.getIsChange() == null?\"\":(entity.getIsChange()==0?\"否\":\"是\"))")
    FinancePaymentChangeLogExcelVO convertLogExcel(FinancePaymentChangeLogVO entity);
    List<FinancePaymentChangeLogExcelVO> convertLogExcelList(List<FinancePaymentChangeLogVO> logVOList);


    FinancePaymentChangeLogDO convertLogDo(FinanceBankTransferDO financeBankTransferDO );

    @Mapping(target = "statusDesc", expression = "java(com.xiaoshuidi.cloud.module.contract.enums.bill.OrderPayStatusEnum.getMsgByCode(orderFeeDetailVO.getPayStatus()))")
    @Mapping(target = "amount", source = "actualPaymentAmount")
    @Mapping(target = "status", source = "payStatus")
    FinanceOrderFeeVO convertFee(OrderFeeDetailVO orderFeeDetailVO);
    List<FinanceOrderFeeVO> convertFeeList(List<OrderFeeDetailVO> orderFeeDetailVOs);
}
